package cn.itcast.Rainbow_self_mention.Activity;

import android.Manifest;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.Rainbow_self_mention.R;
import cn.itcast.Rainbow_self_mention.Dao.SqlHelper;


public class Initial extends AppCompatActivity {

    TextView tv1, tv2, time;
    RelativeLayout la;
    int per = 0;//用于判断该显示Home还是Login

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        la = findViewById(R.id.layout);
        tv1 = findViewById(R.id.font);
        AssetManager am = getAssets();
        Typeface tf = Typeface.createFromAsset(am, "yt.ttf");//字体设置
        tv1.setTypeface(tf);
        tv2 = findViewById(R.id.font2);
        tf = Typeface.createFromAsset(am, "py.ttf");//字体设置
        tv2.setTypeface(tf);
        time = findViewById(R.id.time);
        SqlHelper hp = new SqlHelper(this);
        SQLiteDatabase sq = hp.getReadableDatabase();
        Cursor cursor = sq.query("logined", null, null, null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() == 0) {
            sq = hp.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("_id", 1);
            values.put("username", "null1");
            values.put("userphone", "null1");
            values.put("userid", "0");
            sq.insert("logined", null, values);
            sq = hp.getReadableDatabase();

        }
        cursor = sq.query("chose", null, null, null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() == 0) {
            sq = hp.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("_id", 1);
            values.put("which", 0);
            sq.insert("chose", null, values);
            sq = hp.getReadableDatabase();

        }
        cursor = sq.query("HISTORY", null, null, null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() == 0) {
            sq = hp.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("_id", 1);
            values.put("NUMBER", "null1");
            values.put("NAME_OF_SUBMIT", "null1");
            values.put("PHONE_OF_SUBMIT", "null1");
            values.put("TIME_OF_ORDER ", "null1");
            values.put("TIME_OF_SUBMIT", "null1");
            values.put("NAME_OF_RECEIVER", "null1");
            values.put("PHONE_OF_RECEIVER", "null1");
            values.put("TIME_OF_RECEIVER", "null1");
            values.put("LOCATION", "null1");
            values.put("NAME_OF_PRODUCTION", "null1");
            values.put("NAME_OF_PICTURE", "null1");
            values.put("RECEIVE_OR_NOT", 0);
            sq.insert("HISTORY", null, values);

        }

        cursor = sq.query("box", null, null, null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() == 0) {
            sq = hp.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("_id", 1);
            values.put("box_id", "null1");
            values.put("location", "null1");
            values.put("longitude", 0);
            values.put("lantitude", 0);
            values.put("distance", 0);
            sq.insert("box", null, values);

        }
        sq.close();
        cursor.close();
        sq = hp.getReadableDatabase();
        cursor = sq.query("logined", null, null, null, null, null, null);
        cursor.moveToFirst();

        if (cursor.getInt(cursor.getColumnIndex("userid")) != 0) {
            sq.close();
            cursor.close();
            per = 1;
        } else {
            sq.close();
            cursor.close();
            per = 2;
        }
        permission();
    }

    public void permission() {
        boolean isopengps;
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        isopengps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        String[] permissions = new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        };
        List<String> mPermissionList = new ArrayList<>();
        int mRequestCode = 100;
        mPermissionList.clear();
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        if (mPermissionList.size() > 0) {
            ActivityCompat.requestPermissions(this, permissions, mRequestCode);
        } else if (!isopengps) {
            new AlertDialog.Builder(this).setTitle("open GPS")
                    .setMessage("go to open")
                    //  取消选项
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(Initial.this, "close", Toast.LENGTH_SHORT).show();
                            // 关闭dialog
                            dialogInterface.dismiss();

                        }
                    })
                    //  确认选项
                    .setPositiveButton("setting", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //跳转到手机原生设置页面
                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivityForResult(intent, 1);
                        }
                    })
                    .setCancelable(false)
                    .show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            onResume();
        }
    }

    class Transaction1 extends Thread {
        @Override
        public void run() {
            super.run();
            threadwait(1);
            Intent intent = new Intent(Initial.this, Home.class);
            startActivity(intent);
            finish();
        }
    }

    public void threadwait(int time) {
        try {
            Thread.sleep(time * 500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    protected void onResume() {
        super.onResume();
        if (per == 1) {
            Transaction1 t = new Transaction1();
            t.start();
            la.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Initial.this, Home.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
        if (per == 2) {
            la.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Initial.this, Login.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
    }
}